using Microsoft.Extensions.Logging;
using Vin.Extension.LoggerNotify.Options;
using Vin.Tool.AspNetCore.Singleton;
using Vin.Tool.Domain.ExceptionModel;

namespace Vin.Extension.LoggerNotify.Logger;

public abstract class BaseLogger : ILogger
{
    protected NotifyOption Option => VinApp.GetConfig<NotifyOption>();

    protected List<LogLevel> EnableLevels => Option.EnableLevels.Count <= 0
        ? new List<LogLevel> { LogLevel.Warning, LogLevel.Error, LogLevel.Critical }
        : Option.EnableLevels;

    public virtual IDisposable? BeginScope<TState>(TState state) where TState : notnull => null;

    public virtual bool IsEnabled(LogLevel logLevel) => true;

    public virtual bool CheckIsNotify(LogLevel logLevel)
    {
        if (EnableLevels.Count <= 0 || !EnableLevels.Contains(logLevel)) return false;
        return true;
    }

    public virtual bool CheckIsNotify(Exception? exception)
    {
        return exception is not KnownException;
    }

    public abstract void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception? exception,
        Func<TState, Exception?, string> formatter);
}